package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2022/2/25
 * Time: 21:52
 * Description:
 */
public class DelOperation implements IOperation{

    public void work(BookList bookList) {
        System.out.println("删除图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要删除的书籍:");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //如果index == -1 说明没有这本书
        if(index == -1) {
            System.out.println("没有你要删除的图书！");
            return;
        }
        //这里开始删除
        for (int i = index; i < currentSize-1; i++) {
            //bookList[i] = bookList[i+1];
            Book book = bookList.getBook(i+1);
            bookList.setBookList(i,book);
        }
        bookList.setUsedSize(currentSize-1);
    }
}
